package com.link.base.base.wechat.controller;

import com.link.base.base.wechat.model.WxImage;
import com.link.base.base.wechat.service.ArticlesService;
import com.link.base.base.wechat.service.WxImageService;
import com.link.core.cllog.LCLogger;
import com.link.base.wechat.wxplatform.model.WxPlatform;
import com.link.base.wechat.wxplatform.service.WxPlatformService;
import com.link.base.wechat.wxtoken.service.WxTokenService;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信公众号图片素材Controller
 *
 * @author lujiang
 * @date 2019/02/26 11:00
 */
@Controller
@RequestMapping("/link/wximage")
public class WxImageController extends BasicController<WxImage> {
    @Resource
    private ArticlesService articlesService;
    @Resource
    private WxImageService wxImageService;
    @Resource
    private WxPlatformService wxPlatformService;
    @Resource
    private WxTokenService wxTokenService;

    @Override
    public BasicService<WxImage> getBasicService() throws Exception {
        return wxImageService;
    }

    /**
     * 保存数据库并上传微信服务器，type:img
     *
     * @author lujiang
     * @date 2018
     * @param wxImage 图片素材对象
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> uploadImg(WxImage wxImage) throws Exception {
        Map<String, Object> map = new HashMap<>(16);

        try {
            map = wxImageService.uploadImg(wxImage);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("mes", e.getMessage());
            LCLogger.withException(e);
        }
        return map;
    }

    /**
     * 批量删除图片
     *
     * @author lujiang
     * @date 2018
     * @param ids 图片id集合
     * @return
     */
    @RequestMapping(value = "/batchRemoveImg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> batchRemoveImg(@JsonParam Long[] ids) {
        Map<String, Object> map = new HashMap<>(16);
        if (ids.length < 1) {
            map.put("success", false);
            return map;
        }
        WxImage wxImage;
        try {
            wxImage = wxImageService.queryById(ids[0]);
            WxPlatform wxPlatform = new WxPlatform();
            wxPlatform.setId(Long.valueOf(wxImage.getWxId()));
            WxPlatform platform = wxPlatformService.queryById(wxPlatform);
            String accessToken = wxTokenService.getWxAccessTokenByAppId(platform.getWx_app_id()).getAccess_token();
            for (int i = 0; i < ids.length; i++) {
                wxImage = wxImageService.queryById(ids[i]);
                map = articlesService.deletePermanentMaterial(accessToken, wxImage.getThumbMediaId());
                if ("0".equals(map.get("errcode").toString())) {
                    wxImageService.deleteimage(wxImage);
                }
            }
            map.put("success", true);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            LCLogger.withException(e);
        }
        return map;

    }

    /**
     * 修改图片分组
     *
     * @author lujiang
     * @date 2018
     * @param id 图片id
     * @param groupid 分组id
     * @return
     */
    @RequestMapping(value = "/updateGroup", method = RequestMethod.POST, produces = "application/json")
    @ResponseBody
    public Map<String, Object> updateGroup(Long id, Long groupid) {
        Map<String, Object> map = new HashMap<>(16);
        WxImage wxImage;
        try {
            wxImage = wxImageService.queryById(id);
            wxImage.setGroupId(groupid);
            wxImageService.update(wxImage);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            LCLogger.withException(e);
        }

        return map;
    }

    /**
     * 删除图片并同步删除微信服务器中相应图片素材
     *
     * @author lujiang
     * @date 2018
     * @param id 图片row_id
     * @return
     */
    @RequestMapping(value = "/removeImg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> removeImg(Long id) {
        Map<String, Object> map = new HashMap<>(16);
        try {
            map = wxImageService.removeImg(id);
            map.put("success", true);
        } catch (Exception e) {
            map.put("success", false);
            map.put("msg", e.getMessage());
            LCLogger.withException(e);
        }
        return map;

    }

    /**
     * 修改图片素材信息
     *
     * @author lujiang
     * @date 2018
     * @param entity 图片素材对象
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Override
    @RequestMapping(value = "/update")
    @ResponseBody
    public Map<String, Object> update(@JsonParam WxImage entity, HttpSession session, HttpServletRequest request,
                                      HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>(16);
        map = wxImageService.updateImage(entity);
        return map;
    }

    /**
     * 批量修改图片信息
     *
     * @author lujiang
     * @date 2018
     * @param wxImageList
     * @param session
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/batchUpdateImage")
    @ResponseBody
    public Map<String, Object> batchUpdateImage(@RequestBody List<WxImage> wxImageList, HttpSession session, HttpServletRequest request,
                                                HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>(16);
        try {
            wxImageService.batchUpdateImage(wxImageList);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 删除图片素材
     *
     * @author lujiang
     * @date 2018
     * @param entity 图片素材对象
     * @param session
     * @param request
     * @param response
     * @return
     */
    @Override
    @RequestMapping(value = "/deleteById")
    @ResponseBody
    public Map<String, Object> deleteById(@JsonParam WxImage entity, HttpSession session, HttpServletRequest request,
                                          HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(16);
        try {
            beforDelete(entity, request);
            checkData(entity, request);
            String base = "base";
            if (!checkExistDataCache(base, session, entity)) {
                throw new ServiceException("PUBLIC-031");
            }
            //对接siebel不需要审计追踪
            String y = "Y";
            wxImageService.deleteimage(entity);

            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
